package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.DeliveryDao;
import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.dao.impl.DeliveryDaoImpl;
import com.foreknow.elm.dao.impl.UserDaoImpl;
import com.foreknow.elm.po.Delivery;
import com.foreknow.elm.service.DeliveryService;
import com.foreknow.elm.util.DBUtil;

import java.util.List;

/**
 * @author 啊滨
 * @date 2023/2/14 14:26
 */
public class DeliveryServiceImpl implements DeliveryService {
    @Override
    public List<Delivery> listDeliveryAddressByUserId(String userId) {
        List<Delivery> list=null;

        try {
            DeliveryDao deliveryDao=new DeliveryDaoImpl();
            DBUtil.getConnection();
            list=deliveryDao.listDeliveryAddressByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public Delivery getDeliveryAddressById(Integer daId) {
        Delivery delivery=new Delivery();
        try {
            DeliveryDao deliveryDao=new DeliveryDaoImpl();
            DBUtil.getConnection();
            delivery = deliveryDao.getDeliveryAddressById(daId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return delivery;
    }

    @Override
    public int saveDeliveryAddress(String contactName, Integer contactSex, String contactTel, String address, String userId) {
        int isRight=0;

        try {
            DBUtil.getConnection();
            //开启一个事务
            DBUtil.beginTransaction();
            DeliveryDao deliveryDao=new DeliveryDaoImpl();
            UserDao userDao=new UserDaoImpl();
            int userById = userDao.getUserById(userId);
            if (userById !=0){
                isRight = deliveryDao.saveDeliveryAddress(contactName, contactSex, contactTel, address, userId);
            }
            //提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                //回滚事务
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            //关闭资源
            DBUtil.close();
        }
        return isRight;
    }

    @Override
    public int updateDeliveryAddress(Integer daId, String contactName, Integer contactSex, String contactTel, String address, String userId) {
        int isRight=0;

        try {
            DBUtil.getConnection();
            //开启一个事务
            DBUtil.beginTransaction();
            DeliveryDao deliveryDao=new DeliveryDaoImpl();
            UserDao userDao=new UserDaoImpl();
            int userById = userDao.getUserById(userId);
            if (userById !=0){
                isRight = deliveryDao.updateDeliveryAddress(daId,contactName, contactSex, contactTel, address, userId);
            }
            //提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                //回滚事务
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            //关闭资源
            DBUtil.close();
        }
        return isRight;
    }

    @Override
    public int removeDeliveryAddress(Integer daId) {
        int isRight=0;

        try {
            DBUtil.getConnection();
            //开启一个事务
            DBUtil.beginTransaction();
            DeliveryDao deliveryDao=new DeliveryDaoImpl();
            isRight = deliveryDao.removeDeliveryAddress(daId);
            //提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                //回滚事务
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            //关闭资源
            DBUtil.close();
        }
        return isRight;
    }
}
